# -*- makefile -*-

KLIPSD=${OPENSWANSRCDIR}/linux/include
KLIPSSRCDIR=${OPENSWANSRCDIR}/linux/net/ipsec
MANSRCDIR=${OPENSWANSRCDIR}/lib/libopenswan

include ${OPENSWANSRCDIR}/Makefile.common

LEX=flex
ifeq ($(USE_YACC),true)
BISON=yacc -b parser
else
BISON=bison
endif

VPATH+= ${KLIPSSRCDIR}
OSDEP?=$(shell uname -s | tr 'A-Z' 'a-z')

# NSS things
DEFINES+=${NSS_FLAGS}    ${FIPS_FLAGS}
INCLUDES+=${NSS_HDRDIRS}  ${FIPS_HDRDIRS}

# Original flags
INCLUDES+=-I. -I${KLIPSSRCDIR} -I${KLIPSD} -I${OPENSWANSRCDIR}
INCLUDES+=-I${OPENSWANSRCDIR}/include
CFLAGS+=${PORTINCLUDE} ${INCLUDES} ${CROSSFLAGS}
CFLAGS+=-DDEBUG
CFLAGS+= $(USERCOMPILE)
ifneq ($(BUILDENV),darwin)
CFLAGS+= -Wall
endif
#CFLAGS+= -Wconversion
#CFLAGS+= -Wmissing-prototypes
CFLAGS+= -Wpointer-arith
CFLAGS+= -Wcast-qual
#CFLAGS+= -Wmissing-declarations
CFLAGS+= -Wstrict-prototypes
#CFLAGS+= -pedantic
#CFLAGS+= -W
#CFLAGS+= -Wwrite-strings
CFLAGS+= -Wbad-function-cast
CFLAGS+= ${DEFINES}

ifeq ($(USE_NAT_TRAVERSAL),true)
CFLAGS+= -DNAT_TRAVERSAL
endif

ifeq ($(USE_KLIPS),true)
CFLAGS+= -DKLIPS
endif

ifeq ($(USE_IKEv1),true)
CFLAGS+= -DIKEV1
endif

ARFLAGS=crvs
MANS=

.PHONY:	all install clean l t lt tar check depend checkprograms

# we can't do tests with two conditions, but we can concatenate the strings
SHOULDWERESTART=${USE_OBJDIR}$(wildcard ${ONEFILE})
ifeq ($(SHOULDWERESTART),true${ONEFILE})
all programs clean install:
	@echo OBJDIR MAKE $@
	@cd ${OPENSWANSRCDIR} && cd ${OBJDIRTOP}/lib/lib${LIBRARY} && ${MAKE} $@
else
all:	$(LIB)
programs: $(LIB)
clean:	cleanall
install: doinstall

endif

doinstall:
	@true

# really, installing man pages for libraries on production machines is stupid.
doinstall_devel:
	@for d in $(MANDIR); do mkdir -p $$d; done
	@for f in $(MANS) ; \
	do \
		$(INSTALL) $(INSTMANFLAGS) $(MANSRCDIR)/$$f $(MANDIR)/ipsec_$$f || exit 1 ; \
	done
	$(OPENSWANSRCDIR)/packaging/utils/manlink $(foreach man, $(MANS), ${MANSRCDIR}/$(man)) | \
	while read from to; \
	do \
                rm -f $(MANDIR)/$$to;; \
		ln -s -f ipsec_$$from $(MANDIR)/$$to; \
	done


install_file_list:
	@for f in $(MANS) ; \
	do \
		echo $(MANDIR)/ipsec_$$f;\
	done;
	@$(OPENSWANSRCDIR)/packaging/utils/manlink $(foreach man, $(MANS), ${MANSRCDIR}/$(man)) | \
	while read from to; \
	do \
		echo $(MANDIR)/$$to; \
	done

$(LIB): $(OBJS)
	@echo AR ${LIB}
	@$(AR) $(ARFLAGS) $(LIB) $(OBJS)

$(OBJS):	$(HDRS)

%.o: %.c
	@echo CC $(notdir $<)
	@$(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<

cleanall::
	rm -rf ${OBJS} $(LIB)

# Gather dependencies caused by explicit #includes within .c files
depend: ${DISTSRC}
	@(ls $(DISTSRC) | grep '\.c' | xargs $(CC) -MM ${PORTINCLUDE} ${INCLUDES} ${ALLFLAGS} ) | sed -e 's,${OPENSWANSRCDIR},../..,g' >Makefile.depend.${OSDEP}

-include ${srcdir}Makefile.depend.${OSDEP}


